//
//  Extension.swift
//  WeiBoSwift
//
//  Created by hgj on 2016/11/16.
//  Copyright © 2016年 HuGuoJian. All rights reserved.
//  延展/分类

import UIKit

// MARK: -----UILabel + Extension-----
extension UILabel {
    
    convenience init(title: String, titleColor: UIColor,fontSize: CGFloat) {
        self.init()
        
        text = title
        textColor = titleColor
        font = UIFont.systemFont(ofSize: fontSize)
    }
}

// MARK: -----UIButton + Extension-----
extension UIButton {
    
    convenience init(title: String, titleColor: UIColor, fontSize: CGFloat) {
        self.init()
        
        setTitle(title, for: .normal)
        setTitleColor(titleColor, for: .normal)
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
    }
}

// MARK: -----UIBarButtonItem + Extension-----
extension UIBarButtonItem {
    //title: String = "", imageName: String = ""
    convenience init(title: String = "", imageName: String? = nil,target: Any?, action: Selector?) {
        self.init() // ===>注意点
        
        let btn = UIButton()
        
        // 文字
        btn.setTitle(title, for: .normal)
        
        // 颜色
        btn.setTitleColor(#colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1), for: .normal)
        btn.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: .highlighted)
        
        // 字体
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        
        // 图片  ===>因为是可变 所以要判断<===
        if let img = imageName {
            btn.setImage(UIImage(named: img), for: .normal)
            btn.setImage(UIImage(named:img + "_highlighted"), for: .highlighted)
        }
        
        // frame
        btn.sizeToFit()
        
        // 监听  ===>因为是可变 所以要判断<===
        if let ac = action {
            btn.addTarget(target, action: ac, for: .touchUpInside)
        }
        
        // ===>便利构造函数注意点
        self.customView = btn
    }
}

// MARK: ----- UIImage + Extension -----
extension UIImage {
    
    // 写一个截屏的类方法
    class func snapShortCurrentWindow() -> UIImage {
        
        //
        let window = UIApplication.shared.keyWindow!
        
        // 开启上下文
        UIGraphicsBeginImageContextWithOptions(window.frame.size, false, 0)
        
        // 绘制
        window.drawHierarchy(in: window.frame, afterScreenUpdates: true)
        
        // 获取裁剪完毕的图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        // 关闭上下文
        UIGraphicsEndImageContext()
        
        return image!
    }
    
    
    
    //将图片缩放到指定宽度
    func scaleImage(width: CGFloat) -> UIImage{
        let imageW = self.size.width
        let imageH = self.size.height
        //需要判断 如果图片的宽度小于缩放的宽度 就不需要缩放
        if imageW < width {
            return self
        }
        
        let scaleH = imageH / imageW * width
        let imageBounds = CGRect(x: 0, y: 0, width: width, height: scaleH)
        //开启图片的上下文
        UIGraphicsBeginImageContextWithOptions(imageBounds.size, false, 0)
        //将图片绘制上下文中
        self.draw(in: imageBounds)
        
        //从上下文中获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        //关闭图片的上下文
        UIGraphicsEndImageContext()
        return image!
    }
    
}

// MARK: - ****** UIView + Extension ******
extension UIView {
    
    //视图层次嵌套比较深的时候 使用代理或者闭包的时候会比较麻烦 可以使用这种方式来解决
    //遍历响应者链条 查找对应的控制器(导航, tabbarVC,tableVC)
    
    //查找导航视图控制器
    func findNavController() -> UINavigationController? {
        
        //1. 获取当前控件的下一个响应者
        var responder = self.next
        
        while responder != nil {
            if let obj = responder as? UINavigationController {
                return obj
            }
            
            //如果不是  就查找下一个响应者的下一个响应者
            responder = responder?.next
        }
        
        return nil

    }
}





















